﻿using System;
using System.Windows.Forms;
using BasicFramework.Entities;
using BasicFramework.Scenes;
using BasicFrameworkComponentLibrary;
using BasicFrameworkComponentLibrary.DirectX9.Components.Cameras;
using BasicFrameworkComponentLibrary.GeneralComponents.InputComponents;
using Demo_App.Components;
using SlimDX;
using SlimDX.RawInput;
using BasicFrameworkComponentLibrary.DirectX9Components.HelperComponents;

namespace Demo_App.Scenes
{
    public class IntroScene : Scene
    {
        private Entity camera;
        private Entity flyingCube;
        private Entity helper;
        private Entity grid;
        public Entity input;


        public IntroScene(string name, Guid guid)
            : base(name, guid)
        {
        }

        public IntroScene(string name)
            : base(name)
        {
        }


        public override void Initialize()
        {
            base.Initialize();

            grid = EntityFactory.Create<Entity>("Grid", new Grid());

            helper = EntityFactory.Create<Entity>("Helper", new Graticule());

            camera = EntityFactory.Create<Entity>("FixedCamera", new FixedCamera());

            input = EntityFactory.Create<Entity>("Input", new InputReceiver());
            input.Find<InputReceiver>().KeyboardInput += new EventHandler<KeyboardInputEventArgs>(IntroScene_KeyboardInput);

            this.flyingCube = EntityFactory.Create<Entity>("flyingCube", new TPiece());

            FixedCamera fixedCam = camera.Find<FixedCamera>();
            fixedCam.EyePosition = new Vector3(0, 10, -60);
            fixedCam.LookAt = new Vector3(7, 16, 0);

        }

        public override void Start()
        {
            base.Start();


        }

        void IntroScene_KeyboardInput(object sender, SlimDX.RawInput.KeyboardInputEventArgs e)
        {
            if (e.State == KeyState.Pressed)
            {
                if (e.Key == Keys.Up)
                {
                    Transform trans = flyingCube.Find<Transform>();
                    trans.Position = new Vector3(trans.Position.X, trans.Position.Y + 0.5f, trans.Position.Z);
                }
                if (e.Key == Keys.Down)
                {
                    Transform trans = flyingCube.Find<Transform>();
                    trans.Position = new Vector3(trans.Position.X, trans.Position.Y - 0.5f, trans.Position.Z);
                }
                if (e.Key == Keys.Left)
                {
                    Transform trans = flyingCube.Find<Transform>();
                    trans.Position = new Vector3(trans.Position.X - 0.5f, trans.Position.Y, trans.Position.Z);
                }
                if (e.Key == Keys.Right)
                {
                    Transform trans = flyingCube.Find<Transform>();
                    trans.Position = new Vector3(trans.Position.X + 0.5f, trans.Position.Y, trans.Position.Z);
                }
            }

        }

        public override void Update(float frame)
        {
            base.Update(frame);

            if (this.flyingCube.Find<IPiece>().IsSet)
            {
                this.CreateNewFlyingPiece();
            }
        }

        private void CreateNewFlyingPiece()
        {
            this.flyingCube = EntityFactory.Create<Entity>("flyingCube", new TPiece());
        }



        public override void Stop()
        {
            base.Stop();


        }

        public override void Paused()
        {
            base.Paused();
        }

        public override void Resume()
        {
            base.Resume();
        }

    }
}
